package com.example.nsl;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class HelpView extends LinearLayout {
    private ImageView imageView;
    private TextView textView;
    private LinearLayout layoutHelp;
    private ObjectAnimator   objectAnimator;

    public HelpView(@NonNull Context context) {
        super(context);
        initView();
        Log.e("context",""+context.getClass().getName());

    }

    public HelpView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HelpView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public HelpView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private void initView() {
//        setId(R.id.layoutHelp);
         LayoutInflater.from(getContext()).inflate(R.layout.helpview,this,true);
//        LayoutParams layoutParams=   new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//       addView(view,layoutParams);
//        setGravity(Gravity.RIGHT|Gravity.BOTTOM);
        imageView=findViewById(R.id.imageview);
        textView=findViewById(R.id.textView);
        layoutHelp=findViewById(R.id.layoutHelp);
        textView.setOnClickListener(v -> {

        });
        // 设置默认位置显示


        if(textView.getMeasuredWidth()!=0){
           setTranslationX(textView.getMeasuredWidth());
        }else {
            textView.getViewTreeObserver().addOnGlobalLayoutListener(new  ViewTreeObserver.OnGlobalLayoutListener (){
                @Override
                public void onGlobalLayout() {
                    textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    setTranslationX(textView.getMeasuredWidth());
                    layoutHelp.setVisibility(VISIBLE);
                }
            });
        }

        imageView.setOnClickListener(v -> {
            if(objectAnimator!=null && objectAnimator.isRunning()){
                objectAnimator.cancel();
                objectAnimator=null;
            }
            if(getTranslationX()==0f){
                objectAnimator = ObjectAnimator.ofFloat(HelpView.this, "translationX", 0f,textView.getMeasuredWidth());
                objectAnimator.setDuration(200);
                objectAnimator.start();
            }else{
                objectAnimator = ObjectAnimator.ofFloat(HelpView.this, "translationX", textView.getMeasuredWidth(),0f);
                objectAnimator.setDuration(200);
                objectAnimator.start();
            }
        });
    }


    @Override
    protected void onDetachedFromWindow() {
        if(objectAnimator!=null && objectAnimator.isRunning()){
            objectAnimator.cancel();
            objectAnimator=null;
        }
        super.onDetachedFromWindow();
    }
}
